import express from 'express'
import bodyParser from 'body-parser'
import path from 'path'
import access from './middlewares/access'

const resolve = (fpath: string) => path.resolve(__dirname, fpath);

const app = express()

const port = 8000

app.use('/public', express.static('public'));

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(access);


app.use('/api/user', (req, res, next) => {
    if (!req.body.token) {
        res.status(401).json({
            code: -1,
            message: 'fail'
        });

    } else {
        res.send({
            name: 'alice',
            age: 12
        })
    }

})

app.use('/api/book', (req, res, next) => {
    res.cookie('op', 'lufy', { maxAge: 60 * 60 * 24 * 1000 })
    res.send({
        name: 'luck and lucy 8888'
    })
})


app.get('/', (req, res, next) => {
    res.sendFile(resolve('public/index.html'))
})

app.listen(port, () => {
    console.log(`server running at http://localhost:${port}`)
})
